home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / CmdDlgOneString.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  9.4 KB  |  319 lines  |  [TEXT/KAHL]

  1. /* CmdDlgOneString.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "CmdDlgOneString.h"
  31. #include "Memory.h"
  32. #include "Screen.h"
  33. #include "EventLoop.h"
  34. #include "Menus.h"
  35. #include "TextEdit.h"
  36. #include "SimpleButton.h"
  37. #include "WrapTextBox.h"
  38. #include "DataMunging.h"
  39. #include "Main.h"
  40. #include "Alert.h"
  41.  
  42.  
  43. #define WINXSIZE (500)
  44.  
  45. #define PROMPTX (10)
  46. #define PROMPTY (5)
  47. #define PROMPTWIDTH (WINXSIZE - (2 * PROMPTX))
  48. #define PROMPTHEIGHT (50)
  49.  
  50. #define BOXPROMPTX (PROMPTX)
  51. #define BOXPROMPTY (PROMPTY + PROMPTHEIGHT + 5)
  52.  
  53. #define BOXEDITX (BOXPROMPTX + 50)
  54. #define BOXEDITY (BOXPROMPTY - 3)
  55. #define BOXEDITWIDTH (WINXSIZE - BOXEDITX - 5)
  56. #define BOXEDITHEIGHT (60)
  57.  
  58. #define CANCELBUTTONWIDTH (80)
  59. #define CANCELBUTTONHEIGHT (21)
  60. #define CANCELBUTTONX ((1 * WINXSIZE) / 4 - (CANCELBUTTONWIDTH / 2))
  61. #define CANCELBUTTONY (BOXEDITY + BOXEDITHEIGHT + 10)
  62.  
  63. #define OKBUTTONWIDTH (CANCELBUTTONWIDTH)
  64. #define OKBUTTONHEIGHT (CANCELBUTTONHEIGHT)
  65. #define OKBUTTONX ((3 * WINXSIZE) / 4 - (OKBUTTONWIDTH / 2))
  66. #define OKBUTTONY (CANCELBUTTONY)
  67.  
  68. #define WINYSIZE (CANCELBUTTONY + CANCELBUTTONHEIGHT + 20)
  69.  
  70.  
  71. typedef struct
  72.     {
  73.         WinType*                    ScreenID;
  74.         char*                            PromptText;
  75.         char*                            EditPromptText;
  76.         TextEditRec*            EditBox;
  77.         SimpleButtonRec*    OKButton;
  78.         SimpleButtonRec*    CancelButton;
  79.     } WindowRec;
  80.  
  81.  
  82. static void            RedrawWindow(WindowRec* Window)
  83.     {
  84.         CheckPtrExistence(Window);
  85.         TextEditFullRedraw(Window->EditBox);
  86.         RedrawSimpleButton(Window->OKButton);
  87.         RedrawSimpleButton(Window->CancelButton);
  88.         SetClipRect(Window->ScreenID,0,0,WINXSIZE,WINYSIZE);
  89.         DrawWrappedTextBox(Window->ScreenID,Window->PromptText,GetScreenFont(),9,
  90.             PROMPTX,PROMPTY,PROMPTWIDTH,PROMPTHEIGHT);
  91.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,Window->EditPromptText,
  92.             StrLen(Window->EditPromptText),BOXPROMPTX,BOXPROMPTY,ePlain);
  93.     }
  94.  
  95.  
  96. /* present a dialog box that allows the user to edit a single string parameter */
  97. /* returns True if the user changes the value and clicks OK. */
  98. MyBoolean                CommandDialogOneString(char* Prompt, char* BoxName, char** DataInOut)
  99.     {
  100.         WindowRec*        Window;
  101.         MyBoolean            LoopFlag;
  102.         MyBoolean            DoItFlag EXECUTE(= -31342);
  103.         MyBoolean            ReturnValue;
  104.  
  105.         Window = (WindowRec*)AllocPtrCanFail(sizeof(WindowRec),
  106.             "CommandDialogOneString:  WindowRec");
  107.         if (Window == NIL)
  108.             {
  109.              FailurePoint1:
  110.                 AlertHalt("There is not enough memory available to edit the "
  111.                     "command parameters.",NIL);
  112.                 return False;
  113.             }
  114.         Window->PromptText = Prompt;
  115.         Window->EditPromptText = BoxName;
  116.  
  117.         Window->ScreenID = MakeNewWindow(eModelessDialogWindow,eWindowNotClosable,
  118.             eWindowNotZoomable,eWindowNotResizable,DialogLeftEdge(WINXSIZE),
  119.             DialogTopEdge(WINYSIZE),WINXSIZE,WINYSIZE,(void (*)(void*))&RedrawWindow,Window);
  120.         if (Window->ScreenID == NIL)
  121.             {
  122.              FailurePoint2:
  123.                 ReleasePtr((char*)Window);
  124.                 goto FailurePoint1;
  125.             }
  126.         SetWindowName(Window->ScreenID,"Edit Command");
  127.  
  128.         Window->OKButton = NewSimpleButton(Window->ScreenID,"OK",OKBUTTONX,OKBUTTONY,
  129.             OKBUTTONWIDTH,OKBUTTONHEIGHT);
  130.         if (Window->OKButton == NIL)
  131.             {
  132.              FailurePoint3:
  133.                 KillWindow(Window->ScreenID);
  134.                 goto FailurePoint2;
  135.             }
  136.         SetDefaultButtonState(Window->OKButton,True);
  137.  
  138.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",CANCELBUTTONX,
  139.             CANCELBUTTONY,CANCELBUTTONWIDTH,CANCELBUTTONHEIGHT);
  140.         if (Window->CancelButton == NIL)
  141.             {
  142.              FailurePoint4:
  143.                 DisposeSimpleButton(Window->OKButton);
  144.                 goto FailurePoint3;
  145.             }
  146.  
  147.         Window->EditBox = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),9,
  148.             BOXEDITX,BOXEDITY,BOXEDITWIDTH,BOXEDITHEIGHT);
  149.         if (Window->EditBox == NIL)
  150.             {
  151.              FailurePoint5:
  152.                 DisposeSimpleButton(Window->CancelButton);
  153.                 goto FailurePoint4;
  154.             }
  155.         TextEditNewRawData(Window->EditBox,*DataInOut,SYSTEMLINEFEED);
  156.         TextEditHasBeenSaved(Window->EditBox);
  157.  
  158.  
  159.         EnableTextEditSelection(Window->EditBox);
  160.         LoopFlag = True;
  161.         while (LoopFlag)
  162.             {
  163.                 OrdType                            X;
  164.                 OrdType                            Y;
  165.                 ModifierFlags                Modifiers;
  166.                 MenuItemType*                MenuItem;
  167.                 char                                KeyPress;
  168.  
  169.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  170.                     {
  171.                         default:
  172.                             break;
  173.                         case eCheckCursor:
  174.                             if (TextEditIBeamTest(Window->EditBox,X,Y))
  175.                                 {
  176.                                     SetIBeamCursor();
  177.                                 }
  178.                              else
  179.                                 {
  180.                                     SetArrowCursor();
  181.                                 }
  182.                             goto UpdateCursorPoint;
  183.                             break;
  184.                         case eNoEvent:
  185.                          UpdateCursorPoint:
  186.                             TextEditUpdateCursor(Window->EditBox);
  187.                             break;
  188.                         case eMenuStarting:
  189.                             EnableMenuItem(mPaste);
  190.                             if (TextEditIsThereValidSelection(Window->EditBox))
  191.                                 {
  192.                                     EnableMenuItem(mCut);
  193.                                     EnableMenuItem(mCopy);
  194.                                     EnableMenuItem(mClear);
  195.                                 }
  196.                             EnableMenuItem(mSelectAll);
  197.                             if (TextEditCanWeUndo(Window->EditBox))
  198.                                 {
  199.                                     EnableMenuItem(mUndo);
  200.                                 }
  201.                             break;
  202.                         case eMenuCommand:
  203.                             if (MenuItem == mPaste)
  204.                                 {
  205.                                     TextEditDoMenuPaste(Window->EditBox);
  206.                                 }
  207.                             else if (MenuItem == mCut)
  208.                                 {
  209.                                     TextEditDoMenuCut(Window->EditBox);
  210.                                 }
  211.                             else if (MenuItem == mCopy)
  212.                                 {
  213.                                     TextEditDoMenuCopy(Window->EditBox);
  214.                                 }
  215.                             else if (MenuItem == mClear)
  216.                                 {
  217.                                     TextEditDoMenuClear(Window->EditBox);
  218.                                 }
  219.                             else if (MenuItem == mUndo)
  220.                                 {
  221.                                     TextEditDoMenuUndo(Window->EditBox);
  222.                                     TextEditShowSelection(Window->EditBox);
  223.                                 }
  224.                             else if (MenuItem == mSelectAll)
  225.                                 {
  226.                                     TextEditDoMenuSelectAll(Window->EditBox);
  227.                                 }
  228.                             else
  229.                                 {
  230.                                     EXECUTE(PRERR(AllowResume,
  231.                                         "CommandDialogOneString: Undefined menu option chosen"));
  232.                                 }
  233.                             break;
  234.                         case eKeyPressed:
  235.                             if ((KeyPress == 13) && ((Modifiers & eCommandKey) == 0))
  236.                                 {
  237.                                     FlashButton(Window->OKButton);
  238.                                     DoItFlag = True;
  239.                                     LoopFlag = False;
  240.                                 }
  241.                             else if (KeyPress == eCancelKey)
  242.                                 {
  243.                                     FlashButton(Window->CancelButton);
  244.                                     DoItFlag = False;
  245.                                     LoopFlag = False;
  246.                                 }
  247.                             else
  248.                                 {
  249.                                     TextEditDoKeyPressed(Window->EditBox,KeyPress,Modifiers);
  250.                                 }
  251.                             break;
  252.                         case eMouseDown:
  253.                             if (SimpleButtonHitTest(Window->OKButton,X,Y))
  254.                                 {
  255.                                     if (SimpleButtonMouseDown(Window->OKButton,X,Y,NIL,NIL))
  256.                                         {
  257.                                             DoItFlag = True;
  258.                                             LoopFlag = False;
  259.                                         }
  260.                                 }
  261.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  262.                                 {
  263.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  264.                                         {
  265.                                             DoItFlag = False;
  266.                                             LoopFlag = False;
  267.                                         }
  268.                                 }
  269.                             else if (TextEditHitTest(Window->EditBox,X,Y))
  270.                                 {
  271.                                     TextEditDoMouseDown(Window->EditBox,X,Y,Modifiers);
  272.                                 }
  273.                             break;
  274.                     }
  275.             }
  276.         ERROR((DoItFlag != True) && (DoItFlag != False),PRERR(ForceAbort,
  277.             "CommandDialogOneString:  DoItFlag is neither true nor false"));
  278.  
  279.         ReturnValue = False;
  280.  
  281.         if (DoItFlag)
  282.             {
  283.                 MyBoolean                    ErrorNoMemory;
  284.                 char*                            StringTemp;
  285.  
  286.                 ErrorNoMemory = False;
  287.  
  288.                 if (TextEditDoesItNeedToBeSaved(Window->EditBox))
  289.                     {
  290.                         StringTemp = TextEditGetRawData(Window->EditBox,SYSTEMLINEFEED);
  291.                         if (StringTemp == NIL)
  292.                             {
  293.                                 ErrorNoMemory = True;
  294.                             }
  295.                          else
  296.                             {
  297.                                 ReleasePtr(*DataInOut);
  298.                                 *DataInOut = StringTemp;
  299.                                 SetTag(StringTemp,"CommandDialogOneString return value");
  300.                                 ReturnValue = True;
  301.                             }
  302.                     }
  303.  
  304.                 if (ErrorNoMemory)
  305.                     {
  306.                         AlertHalt("There was not enough memory available to save all of "
  307.                             "the attributes.",NIL);
  308.                     }
  309.             }
  310.  
  311.         DisposeSimpleButton(Window->OKButton);
  312.         DisposeSimpleButton(Window->CancelButton);
  313.         DisposeTextEdit(Window->EditBox);
  314.         KillWindow(Window->ScreenID);
  315.         ReleasePtr((char*)Window);
  316.  
  317.         return ReturnValue;
  318.     }
  319.